<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTool :node-data="company" @openDialog="openDialog" />
        <el-tree v-loading="loading" :data="departs" :props="defaultProps" default-expand-all>
          <!-- 这个tree遍历了数组，把数组的每项的data传了出来 -->
          <template #default="{ data }">
            <!-- tree节点的宽度默认是有宽度撑开的, 这里给row加样式为什么行？？ -->
            <TreeTool
              :node-data="data"
              :is-more-select="isMoreSelect"
              @getDepartmentsList="getDepartmentsList"
              @openDialog="openDialog"
              @openDialogEdit="openDialogEdit"
            />
          </template>
        </el-tree>
      </el-card>
      <el-dialog :title="title" :visible="dialogTableVisible" @close="closeDialog">
        <AddDepartment ref="addDepartment" :node-data="nodeData" :departs-list="departsList" @closeDialog="closeDialog" @getDepartmentsList="getDepartmentsList" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import TreeTool from '@/views/departments/components/tree-tools.vue'
import { reqGetDepartmentsList } from '@/api/departments'
import { listToTree } from '@/utils'
import AddDepartment from '@/views/departments/components/addDepartment.vue'

export default {
  name: 'Departments',
  components: {
    TreeTool,
    AddDepartment
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name'

      },
      company: { name: '你的潜力，是改变世界的动力', manager: 'hope', id: '' },
      isMoreSelect: true,
      dialogTableVisible: false,
      nodeData: {},
      departsList: [],
      title: '',
      loading: false

    }
  },

  created() {
    this.getDepartmentsList()
  },
  methods: {
    async  getDepartmentsList() {
      this.loading = true
      // await new Promise((resolve, reject) => {
      //   setTimeout(() => {
      //     resolve()
      //   }, 5000)
      // })
      const res = await reqGetDepartmentsList()
      this.departsList = res.depts
      this.departs = listToTree(res.depts)
      this.loading = false
    },
    closeDialog() {
      this.$refs.addDepartment.reset()
      this.dialogTableVisible = false
    },
    openDialog(nodeData) {
      this.dialogTableVisible = true
      this.nodeData = nodeData
      this.title = '新增部门'
    },
    async openDialogEdit(nodeData) {
      // 需求：当点击编辑按钮时需要数据的回显
      // 1.TreeTool触发父组件的openDialogEdit的事件
      // 2.父组件调用子组件的add的接口方法获取回显数据，并进行赋值
      this.title = '编辑部门'

      this.dialogTableVisible = true
      this.nodeData = nodeData
      await this.$nextTick()
      this.$refs.addDepartment.queryDepartmentDetail()
    }
  }
}
</script>

<style scoped lang="scss">
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
